﻿using System.Text.RegularExpressions;

namespace Best.SmartCms.Utility
{
    /// <summary>
    /// 正则相关处理
    /// </summary>
    public static class RegexHelp
    {
        /// <summary>
        /// 密码规则
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool CheckPass(this string value)
        {
            return CheckRegex(value, @"^(?=.*\d)(?=.*[a-zA-Z]).{8,255}$");
        }
        /// <summary>
        /// 纯数字
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsNumber(this string value)
        {
            return CheckRegex(value, "^[0-9]+$");
        }
        /// <summary>
        /// 数字与英文
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsNumAndLetter(this string value)
        {
            return CheckRegex(value, "^[0-9a-zA-Z]+$");
        }
        /// <summary>
        /// 英文
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsLetter(this string value)
        {
            return CheckRegex(value, "^[a-zA-Z]+$");
        }

        /// <summary>
        /// 包含汉字
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool HasChinese(this string value)
        {
            return CheckRegex(value, @"[\u4e00-\u9fa5]+");
        }
        /// <summary>
        /// 含有特殊字符
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool SpecialLetter(this string value)
        {
            return CheckRegex(value, @"[`~!@#$%^&*()+=|{}':;',\[\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？\\]+");
        }
        /// <summary>
        /// 判断是否来自小程序
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsMiniProgram(this string value)
        {
            return CheckRegex(value, ".*(iPhone|Android).*MicroMessenger.*");
        }
        private static bool CheckRegex(string value, string regex)
        {
            if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(regex))
            {
                return false;
            }
            return Regex.IsMatch(value, regex, RegexOptions.IgnoreCase);
        }

        /// <summary>
        /// 正则匹配手机号
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static bool MatchPhone(this string phone)
        {
            if (string.IsNullOrEmpty(phone))
            {
                return false;
            }
            string exp = @"^(13[0-9]|14[1-9]|15[0-9]|17[0-9]|18[0-9]|19[8|9]|16[0-9])\d{8}$";
            return Regex.IsMatch(phone, exp);
        } 
        
        /// <summary>
        /// 正则匹配电话
        /// </summary>
        /// <param name="tel"></param>
        /// <returns></returns>
        public static bool MatchTel(this string tel)
        {
            if (string.IsNullOrEmpty(tel))
            {
                return false;
            }
            string exp = @"^((\d{1,4}-\d{7,8})|\d{7,11}|(\d{1,5}-\d{1,8}-\d{1,8}))$";
            return Regex.IsMatch(tel, exp);
        }

        /// <summary>
        /// 正则匹配email
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public static bool MatchEamil(this string email)
        {
            if (string.IsNullOrEmpty(email))
            {
                return false;
            }
            string exp = @".*@.*[.]{1}.*";
            return Regex.IsMatch(email, exp);
        }
    }
}
